package com.cskaoyan.mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.lang.model.element.NestingKind;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ZhidaFeng on 2020/5/1
 **/
public class AdminDaoImpl implements AdminDao {
    @Override
    public int login(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

        try {
            Admin ad = runner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),admin.getEmail(),admin.getPwd());
            if (ad != null){
                return 200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
              return 500;

    }

    @Override
    public List<Admin> allAdmins() throws SQLException {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        return admins;
    }

    @Override
    public List<Admin> getSearchAdmins(Admin params){
        Map<String,Object> dynamicSql = getDynamicSqlParams(params);
        String sql = (String) dynamicSql.get("sql");
        List<String> paramsList = (List<String>) dynamicSql.get("params");
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;

        try {
            admins =  runner.query(sql, new BeanListHandler<Admin>(Admin.class), paramsList.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public void deleteAdmins(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

        try {
            runner.update("delete from admin where id = ?",id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Admin getAdminInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
           admin = runner.query("select * from admin where id = ?",new BeanHandler<>(Admin.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public void updateAdminss(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = runner.query("select email from admin where id = ?",new BeanHandler<>(Admin.class),admin.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (!admin.getEmail().equals(query.getEmail())){
            try {
                runner.update("update admin set email = ? where id = ?",
                        admin.getEmail(),
                        admin.getId());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        try {
            query = runner.query("select nickname from admin where id = ?", new BeanHandler<>(Admin.class),admin.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (!admin.getNickname().equals(query.getNickname())){
            try {
                runner.update("uqdate admin set nickname = ? where id = ?",admin.getNickname(),admin.getId());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        try {
            query = runner.query("select pwd from admin where id = ?", new BeanHandler<>(Admin.class),admin.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (!admin.getPwd().equals(query.getPwd())){
            try {
                runner.update("update admin set pwd = ? where id = ?",admin.getPwd(),admin.getId());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void addAdminss(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.insert("insert into admin values(null,?,?,?)",new BeanHandler<>(Admin.class),
                    admin.getEmail(),admin.getNickname(),admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private Map<String, Object> getDynamicSqlParams(Admin params) {
        String baseSql = "select * from admin where 1 = 1 ";
        List<String> list = new ArrayList<>();
        if (!StringUtils.isEmpty(params.getEmail())){
            baseSql = baseSql + "and email like ? ";
            list.add("%" + params.getEmail() +"%");
        }
        if (!StringUtils.isEmpty(params.getNickname())) {
            baseSql = baseSql + "and nickname like ? ";
            list.add("%" + params.getNickname() +"%") ;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("sql",baseSql);
        map.put("params",list);
        return map;
    }
}
